<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('test_method'))
{
    function user_strtrim($string,$limit) {
        if(strlen($string) > $limit) {
            $ret = substr($string,0,$limit);
            $ret.="...";
        } else {
            $ret = $string;
        }
        
        return $ret;
    }
    
    function user_imgresize($tWidth,$tHeight,$file)
    {
        $filename = $file;
        $percent = 1;
        
        // Get new dimensions
        list($width, $height) = getimagesize($filename);
        $new_width = $width;
        $new_height = $height;
        
        while($new_height > $tHeight OR $new_width > $tWidth) {
            $new_height = $height*$percent;
            $new_width = $width*$percent;
            
            $percent-=0.01;
        }
        
        //Position Computation
        $xpos = ($tWidth-$new_width)/2;
        $ypos = ($tHeight-$new_height)/2;
        
        $file_info = pathinfo($file);
        
        // Resample
        $image_p = imagecreatetruecolor($tWidth, $tHeight);
        $image_bg = imagecolorallocate($image_p,233,240,245);
        $apply_bg = imagefill($image_p,0,0,$image_bg);
        
        switch($file_info['extension']) {
            case "jpg":
                $image = imagecreatefromjpeg($filename);break;
            case "png":
                $image = imagecreatefrompng($filename);break;
            case "gif":
                $image = imagecreatefromgif($filename);break;
            default:
                return null;
        }
        imagecopyresampled($image_p, $image, $xpos, $ypos, 0, 0, $new_width, $new_height, $width, $height);
        
        // Output
        $location = $file;
        
        switch($file_info['extension']) {
            case "jpg":
                imagejpeg($image_p, $location, 100);break;
            case "png":
                imagepng($image_p, $location, 9);break;
            case "gif":
                imagegif($image_p, $location);
        }
        
        $image = file_get_contents($location);
        unlink($location);
        return $image;
    }
    
    function getprivileges($role_id) {
        $query = "SELECT * FROM tbl_privileges WHERE role_id=$role_id";
        $result = mysql_query($query) or die(mysql_error());
        
        $count = 0;
        while($row = mysql_fetch_assoc($result)) {
            $priv[$count] = $row['privilege'];
            $count++;
        }
        return $priv;
    }
    
    function displayPages($tp,$pp) {
        $start = $_GET['page']-($pp/2);
        if($start < 1) {
            $start = 1;
        }
        $end = $start+$pp;
        if($end > $tp) {
            if($tp-$pp < 1) {
                $start = 1;
            } else {
                $start = $tp-$pp;
            }
            $end = $tp;
        }
        $count = $start;
        if($_GET['page'] != 1) {
            $link=current_url().'?page=1';
                
            if(isset($_GET['keywords'])) {
                $link.='&keywords='.$_GET['keywords'];
            }
            if(isset($_GET['searchby'])) {
                $link.='&searchby='.$_GET['searchby'];
            }
            if(isset($_GET['sortby'])) {
                $link.='&sortby='.$_GET['sortby'];
            }
            if(isset($_GET['sorttype'])) {
                $link.='&sorttype='.$_GET['sorttype'];
            }
                
            echo '<input type="button" value="&lt;&lt; First" class="silver-button" onClick="anchor(\''.$link.'\')" />';
        }
        
        if($_GET['page'] != 1) {
            $prev = $_GET['page']-1;
            $link=current_url().'?page='.$prev;
                
            if(isset($_GET['keywords'])) {
                $link.='&keywords='.$_GET['keywords'];
            }
            if(isset($_GET['searchby'])) {
                $link.='&searchby='.$_GET['searchby'];
            }
            if(isset($_GET['sortby'])) {
                $link.='&sortby='.$_GET['sortby'];
            }
            if(isset($_GET['sorttype'])) {
                $link.='&sorttype='.$_GET['sorttype'];
            }
                
            echo '<input type="button" value="&lt; Prev" class="silver-button" onClick="anchor(\''.$link.'\')" />';
        }
        
        while($count<=$end) {
            echo '<input type="button" value="'.$count.'" class="silver-button"';
            if($count == $_GET['page']) {
                echo ' disabled';
            } else {
                $link=current_url().'?page='.$count;
                
                if(isset($_GET['keywords'])) {
                    $link.='&keywords='.$_GET['keywords'];
                }
                if(isset($_GET['searchby'])) {
                    $link.='&searchby='.$_GET['searchby'];
                }
                if(isset($_GET['sortby'])) {
                    $link.='&sortby='.$_GET['sortby'];
                }
                if(isset($_GET['sorttype'])) {
                    $link.='&sorttype='.$_GET['sorttype'];
                }
                
                echo ' onClick="anchor(\''.$link.'\')"';
            }
            echo ' />';
            $count++;
        }
        
        if($_GET['page'] < $tp) {
            $next = $_GET['page']+1;
            $link=current_url().'?page='.$next;
                
            if(isset($_GET['keywords'])) {
                $link.='&keywords='.$_GET['keywords'];
            }
            if(isset($_GET['searchby'])) {
                $link.='&searchby='.$_GET['searchby'];
            }
            if(isset($_GET['sortby'])) {
                $link.='&sortby='.$_GET['sortby'];
            }
            if(isset($_GET['sorttype'])) {
                $link.='&sorttype='.$_GET['sorttype'];
            }
                
            echo '<input type="button" value="Next &gt;" class="silver-button" onClick="anchor(\''.$link.'\')" />';
        }
        
        if($_GET['page'] < $tp) {
            $link=current_url().'?page='.$tp;
                
            if(isset($_GET['keywords'])) {
                $link.='&keywords='.$_GET['keywords'];
            }
            if(isset($_GET['searchby'])) {
                $link.='&searchby='.$_GET['searchby'];
            }
            if(isset($_GET['sortby'])) {
                $link.='&sortby='.$_GET['sortby'];
            }
            if(isset($_GET['sorttype'])) {
                $link.='&sorttype='.$_GET['sorttype'];
            }
                
            echo '<input type="button" value="Last &gt;&gt;" class="silver-button" onClick="anchor(\''.$link.'\')" />';
        }
    }
}